<template>
	<view class="">
		<my-pageMeta ref='pageMeta' @getInfo="setFontSize"></my-pageMeta>
		<view style="display:flex;flex-direction: column;">
			<text>rootFontsize字体大小：{{fontSize}}</text>
			<text>每行容量：{{lineCapacity}}</text>
			<text @click="getChildData">每个元素的宽度：{{inLength}}</text>
			
		</view>
		<view class="as-container" ref="listContainer">
			<view class="flexchild" v-for="(item,index) in 29">{{item}}</view>
			<view v-for="item in paddingLength" class="flexchild">
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				paddingLength: 0,
				fontSize: '',
				inLength:'',//每个元素的宽度
				lineCapacity:'',  //每行能放几个
			}
		},
		mounted() {
			this.reCalcPaddingLength()
			window.onresize = () =>{
				this.reCalcPaddingLength()
			}
		},
		watch: {//观察fontSize的变化
			'fontSize'(e) {
				this.reCalcPaddingLength()
			}
		},
		methods: {
			reCalcPaddingLength() {
					if (this.fontSize) {
					let outerLength = this.$refs.listContainer.$el.clientWidth // 外面div的宽度
					let inLength = (this.fontSize * 10) + 0  //定义每个项目加上内外边距的宽度
					this.inLength=inLength
					let listLen = 29 // 子元素个数，自行调整
					let lineCapacity = Math.floor(outerLength / inLength) // 计算每一行的容量
					this.lineCapacity=lineCapacity
					this.paddingLength = listLen % lineCapacity === 0 ? 0 : (lineCapacity - listLen %
						lineCapacity) // 得出需要添加的空块
				}

			},
			// 获取子组件传来的字体大小 
			setFontSize(e) {
				this.fontSize = e.rootFontsize
			},
			// 利用$refs获取子组件的数据
			getChildData(){
				let windowWidth=this.$refs.pageMeta.getVal().windowWidth
				let rootFontsize=this.$refs.pageMeta.getVal().rootFontsize
				console.log(rootFontsize)
				return rootFontsize
			}
		}


	}
</script>

<style>
	.as-container {
		display: flex;
		background-color: pink;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.flexchild {
		display: flex;
		justify-content: center;
		align-items: center;
		/* margin: 10px;
		padding: 10px; */
		/* border: 1px solid #007AFF; */
		width: 10rem;
		background-color: #FFFFFF;
		font-size: 0.1rem;
	}
</style>
